
//旅游景区管理 -- 旅游景区类型-列表
var pagesize=10;
var pageindex=1;
var sortfild = ""; //排序的关键字
var sortway = ""; //排序：升序、降序
var ttname = "景区-商户类型";
$(function(){

    //时间选择
    timesearch();
    var today=new Date();
    startDate=endDate=today.format('yyyy-MM-dd');
    starttime=endtime=startDate;
    $("#date1").html(startDate+" - "+endDate);
    $(".select_time1 li ").click(function(){
        $(".select_time1 li").removeClass("blue_color");
        $(this).addClass("blue_color");
        var time=$(this).attr("name");
        if(time=="0"){
            startDate=endDate=today.format('yyyy-MM-dd');
        }else if(time=="1"){
            startDate=endDate=new Date(today.getFullYear(),today.getMonth(),today.getDate()-1).format('yyyy-MM-dd');
        }else if(time=="7"){
            startDate=new Date(today.getFullYear(),today.getMonth(),today.getDate()-6).format('yyyy-MM-dd');
            endDate=today.format('yyyy-MM-dd');
        }else if(time=="30"){
            startDate=new Date(today.getFullYear(),today.getMonth(),today.getDate()-29).format('yyyy-MM-dd');
            endDate=today.format('yyyy-MM-dd');
        }
        starttime=startDate;
        endtime=endDate;
        $("#date1").html(startDate+" - "+endDate);
        if(time==""){
            starttime="";
            endtime="";
        }
        //获取数据
        $("#first_button").click();
    })


    //排序
	$(".list_sort_tab").click(function(){
		 sortfild=$(this).attr("value"); 
		if($(this).find("i").hasClass("icon_downs")){
			$(this).find("i").removeClass("icon_downs");
			$(this).find("i").addClass("icon_ups");
			sortway="asc";
		}else{
			$(this).find("i").removeClass("icon_ups");
			$(this).find("i").addClass("icon_downs");
			 sortway="desc";
		}
		 //必须要，否则排序则不能调用该方法  使用该句，当点击排序，就同时点击了 第一页的按钮，自动调用分页的效果，
        $("#first_button").click();
	});
	
	//全选中或全不选
    $("#checkAll").click(function () {
        if ($(this).is(':checked')) {
            $(".checkbox").prop("checked", true);
        } else {
            $(".checkbox").prop("checked", false);
        }
    })
    
    getData();
	
})

function getData(){
    getlist();
}

//数据列表
function getlist() {
    var pagesize = $("#selectPageSize1").text();
    var pageindex = $("#currentPage").text();
    var layLoad=layer.load(2);
    $("#biuuu_city_list").empty();
    $.post('/SceMerType/getSceMertypelist',{"pagesize":pagesize,"pageindex":pageindex,"starttime":starttime,"endtime":endtime,"sortfild":sortfild,"sortway":sortway},function(value){
        layer.close(layLoad);
        if(value.status==0 && value.msg == "ok"){
            var list=value.list;
            var pagecount=value.pagecount;
            var recordcount=value.recordcount;
            $("#totalSize").html(recordcount);
            $("#totalPage").html(pagecount);
            if(list.length==0){
                $("#biuuu_city_list").prepend("<tr class=\"iframe_tr iframe_bg\"><td colspan=\"4\" style=\"width:1126px;text-align:center;\">暂无数据</td></tr>");
            }else{
                var htm="";
                for(var i=0;i<list.length;i++){
                    htm+="<tr>";
                    htm+="<td><input type=\"checkbox\" name="+list[i].sceincType_Merid+" data='"+list[i].sceincType_encryptId+"'  class=\"checkbox\"></td>";
                    htm+="<td><span class='show_tips'>"+list[i].sceincType_desc+"</span></td>";
                    htm+="<td><span class='show_tips'>"+(list[i].sceincType_createtime)+"</span></td>";

                    htm+="<td class='td-manage'>";
                    htm += "<a href=\"javascript:void(0)\" onclick=\"question_edit('"+list[i].sceincType_encryptId+"','"+list[i].sceincType_desc+"')\">修改</a></div>";
                    htm += "<a href=\"javascript:void(0)\" onclick=\"question_del('"+list[i].sceincType_encryptId+"')\">删除</a></div>";
                    htm += "</td>";

                    htm+="</tr>";
                }
                $("#biuuu_city_list").html(htm);

            }
        }else{
            if(value.msg==null||value.msg==""){
                layer.alert("接口出现错误！", {icon: 2})
            }else{
                layer.alert(value.msg, {icon: 2})
            }
        }
    })
}

/*添加*/
function question_add() {
    $("#add_sceincType_encryptId").val("");
    $("#add_sceincType_encryptId").attr("value","");
    $("#add_sceincType_desc").val("");
    ComOpenWin(1, "添加", "auto", 'auto', $("#newlybuildwin"), 1);
}

//编辑
function question_edit(sceincType_encryptId,sceincType_desc) {
    $("#add_sceincType_encryptId").val(sceincType_encryptId);
    $("#add_sceincType_encryptId").attr("value",sceincType_encryptId);
    $("#add_sceincType_desc").val(sceincType_desc);
    ComOpenWin(1, "修改", "auto", 'auto', $("#newlybuildwin"), 1);
}

//保存
function addSceincTypeSave() {
    var sceincType_encryptId = $("#add_sceincType_encryptId").attr("value");
    var sceincType_desc = $("#add_sceincType_desc").val().trim();
    if(sceincType_desc == "" || sceincType_desc == null){
        layer.alert("景区类型不可为空！",{icon:2});
    }else {
        var ii = layer.load(2);
        $.post('/SceMerType/updateSceMertype', {"sceincType_encryptId": sceincType_encryptId, "sceincType_desc": sceincType_desc}, function (data) {
            layer.close(ii);
            if (data.status == 0 && data.msg == 'ok') {
                layer.msg("操作成功！",{time:2000,icon:1});
                getData();
                mclose();
            } else {
                if(data.msg==null||data.msg==""){
                    layer.alert("接口出现错误！", {icon: 2})
                }else{
                    layer.alert(data.msg, {icon: 2})
                }
            }
        })
    }
}

/*删除*/
function question_del(id) {
    layer.confirm('确认要删除吗？', function(index) {
        var ii = layer.load(2);
        $.post('/SceMerType/delSceMertype', {"sceincType_encryptId": id}, function (data) {
            layer.close(ii);
            if (data.status == 0 && data.msg == 'ok') {
                layer.msg("操作成功！",{time:2000,icon:1});
                $("#first_button").click();
                mclose();
            } else {
                if(data.msg==null||data.msg==""){
                    layer.alert("接口出现错误！", {icon: 2})
                }else{
                    layer.alert(data.msg, {icon: 2})
                }
            }
        })
    });
}

//批量删除提交
function delAll() {
    var tidsArry = new Array();
    $('input[class="checkbox"]:checked').each(function(){
        tidsArry.push($(this).attr("data"));//向数组中添加元素
    });
    if (tidsArry.length == 0){
        layer.alert("请至少选择一个要删除内容！",{icon:0});
    }else {
        layer.confirm('确认要删除吗？', function(index) {
            var ii = layer.load(2);
            $.post('/SceMerType/delSceMertype', {"sceincType_encryptId": tidsArry.join(",")}, function (data) {
                layer.close(ii);
                if (data.status == 0 && data.msg == 'ok') {
                    layer.msg("操作成功！",{time:2000,icon:1});
                    $("#first_button").click();
                    mclose();
                } else {
                    if(data.msg==null||data.msg==""){
                        layer.alert("接口出现错误！", {icon: 2})
                    }else{
                        layer.alert(data.msg, {icon: 2})
                    }
                }
            })
        });
    }
}



//时间选择控制器
function timesearch(){

    layui.use('laydate', function () {
        var laydate = layui.laydate;

        //日期范围
        laydate.render({
            elem: '#date1'
            ,range: true
            ,startDate:new Date().format('yyyy-MM-dd')
            ,endDate:new Date().format('yyyy-MM-dd')
            ,btns: ['confirm']
            ,max: time()//设置最大日期为当前日期时间
            ,ready: function(date){
                //默认控件打开的时候调用一下向左点击的事件，则显示的日期范围为左边为上个月月份，右边为当月月份
                $(".laydate-main-list-0 .laydate-prev-m").click();
            }
            ,done: function (value, date, endDate) { //选择好日期的回调
                startDate=starttime= value.substring(0, 10);
                endDate=endtime= value.substring(13, 23);
                $("#date1").html(startDate +" - "+endDate);
                pageindex = 1;
                $(".select_time1 li").removeClass("blue_color");
                //获取数据
                $("#first_button").click();
            }
        });
    });

}
/**
 * 获取当前时间
 * @returns {String}
 */
function time(){
    var myDate = new Date();
    var year=myDate.getFullYear();
    var month=parseInt(myDate.getMonth()+1);
    var day=myDate.getDate();
    var timedate=year+"-"+month+"-"+day;
    return timedate;
};
function time1(){
    var myDate = new Date();
    var year=myDate.getFullYear();
    var month=parseInt(myDate.getMonth()+1);
    var day=parseInt(myDate.getDate()-1);
    var timedate=year+"-"+month+"-"+day;
    return timedate;
};
/**
 * 当前时间段
 */
function timequantum(n){
    var myDate = new Date();
    var year=myDate.getFullYear();
    var month=parseInt(myDate.getMonth()+1);
    var day=myDate.getDate();
    if(n>day){
        if(month>1){
            month=parseInt(month-1);
        }else{
            year=parseInt(year-1);
        }
    }else{
        day=parseInt(day-n);
    }
    var timedate=year+"-"+month+"-"+day;
    return timedate;
}
function DateDiff(sDate1, sDate2) {  //sDate1和sDate2是yyyy-MM-dd格式
    var aDate, oDate1, oDate2, iDays;
    aDate = sDate1.split("-");
    oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);  //转换为yyyy-MM-dd格式
    aDate = sDate2.split("-");
    oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数

    return iDays;  //返回相差天数
}
Date.prototype.format =function(format){
    var o = {
        "M+" : this.getMonth()+1, //month
        "d+" : this.getDate(), //day
        "h+" : this.getHours(), //hour
        "m+" : this.getMinutes(), //minute
        "s+" : this.getSeconds(), //second
        "q+" : Math.floor((this.getMonth()+3)/3), //quarter
        "S" : this.getMilliseconds() //millisecond
    }
    if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
        (this.getFullYear()+"").substr(4- RegExp.$1.length));
    for(var k in o)if(new RegExp("("+ k +")").test(format))
        format = format.replace(RegExp.$1,
            RegExp.$1.length==1? o[k] :
                ("00"+ o[k]).substr((""+ o[k]).length));
    return format;
}